package appceshi.action;

import appceshi.model.User;
import appceshi.service.UserService;
import appceshi.service.impl.UserServiceImpl;

import com.opensymphony.xwork2.ActionSupport;

@SuppressWarnings("serial")
public class RegisterAction extends ActionSupport {
	private String username;
	private String password;
	private String repassword;

	private UserService userService = new UserServiceImpl();

	public String getUsername() {
		return username;
	}

	public void setUsername(String username) {
		this.username = username;
	}

	public String getPassword() {
		return password;
	}

	public void setPassword(String password) {
		this.password = password;
	}

	public String getRepassword() {
		return repassword;
	}

	public void setRepassword(String repassword) {
		this.repassword = repassword;
	}

	public UserService getUserService() {
		return userService;
	}

	public void setUserService(UserService userService) {
		this.userService = userService;
	}

	@Override
	public String execute() throws Exception {
		User user = new User();
		user.setUsername(username);
		user.setPassword(password);
		if (userService.register(user))
			return SUCCESS;
		
		return ERROR;
	}
	
	@Override
	public void validate() {
		if (null == username || username.isEmpty()) 
			addFieldError("username", "用户名不能为空");
		boolean checkPassword = true;
		if (null == password || password.isEmpty()) {
			addFieldError("password", "密码不能为空");
			checkPassword = false;
		}
		if (null == repassword || repassword.isEmpty()) {
			addFieldError("repassword", "请再次输入密码");
			checkPassword = false;
		}
		if (checkPassword && !password.equals(repassword))
			addFieldError("repassword", "两次输入的密码不一致");
	}
}
